package org.display.graphic
{
	
	import flash.events.*;
	import flash.filesystem.File;
	import flash.ui.*;
	import flash.utils.*;
	
	import mx.controls.*;
	import mx.events.*;
	import mx.managers.*;
	
	import org.data.*;
	import org.display.graphic.*;
	import org.display.util.CreateCommandControl;
	import org.enmu.*;
	import org.net.*;
	import org.utils.*;

	
	public class IvHistory extends Iv
	{
		var tab
		var scene
		var sceneTab
		
		var classname:String=""
			
		var workspace:String=""
			
		var subPaths:Array=[]
		
		public function IvHistory(xmlpath:String,tab,scene,sceneTab,workspace)
		{
			this.workspace=workspace
				
			this.tab=tab
			this.scene=scene
			this.sceneTab=sceneTab
			
			super(xmlpath)
			
			
			
		}
		
		public override function init(){			
			
			var symbol:ShapeBase
			var ivs:Array=[]
			
			for(var item in data.item){
				
				var value=data.item[item]
				
				var ivname:String =value.name
				
				var arr:Array=ivname.split("_")
				
				switch(arr[arr.length-1]){
					
					case AbcType.BT:
						symbol=new Bt()
						break
					case AbcType.TF:
						symbol=new Tf()
						break
					case AbcType.MC:
						symbol=new Mc()
						break
					
					case AbcType.MBT:
						symbol=new Mbt()
						break
					
				}
				
				if(arr[arr.length-1]!=AbcType.IV){
					
					if(value.event=="true"){
						symbol.addChild(symbol.square)
					}
					
					symbol.redraw(value.w,value.h)	 
					symbol.setName(arr[0])
					
					
				}else{
					
					symbol=new Iv((workspace+value.path).replace(/\\/gis,"\/"))
					var arrcopy:Array=arr.concat()
					arrcopy.shift()
					arrcopy.pop()
					//trace(arrcopy.join("_")+":"+ivname)
					symbol.setName(arrcopy.join("_"))
					
				}
				
				symbol.x=value.x
				symbol.y=value.y
				addChild(symbol)
				
			}
		}
		
		protected override function loadView(){
			
			var xmlRes:AbcResponder=new AbcResponder()
			
			xmlRes.onResult=function(e,path){
				
				viewdata=e
				
				classname=getViewName().replace("View","")
				
				createMenuItems(["加载模块"+classname])
				
			}
			
			var path:String=(workspace+"/"+String(data.view)).replace(/\./gis,"/").replace(/\\/gis,"\/")+".as"
			
			new AbcHTTPServices(path,xmlRes)		
		}
		
		protected override function loadViewProxy(){
			
			var xmlRes:AbcResponder=new AbcResponder()
			
			xmlRes.onResult=function(e,path){
				
				//addEventSquare(e)
				
			}
			
			var path:String=(workspace+"/"+String(data.view)).replace(/\./gis,"/").replace(/\\/gis,"\/").replace(/\/view\//gis,"/viewProxy/")+"Proxy.as"
			
			new AbcHTTPServices(path,xmlRes)		
		}
		
		function addEventSquare(value:String){
			
			var events:Array=value.match(/(?<=AbcEventDispatcher\.instance\.dispatcher\(").*?(?=")/gis)
			
			for(var i=0;i<events.length;i++){
				
				var str:String=events[i].replace(/\s/gis,"")
				
				//if(str.indexOf("AsynEvent")!=-1||str.indexOf("FunEvent")!=-1){
				
				var square:EventSquare=new EventSquare(str,0xFCF27F,0xFF6600)
				
				this.addChild(square)
				
				square.x=-square.width
					
				square.y=square.height*i
				
				//}
				
			}
			
		}
		
		public function check(obj):Boolean{
			
			var ivs:Array=ElementControl.getObjs(Iv,this)
			
			for(var i=0;i<ivs.length;i++){
				
			//	trace("file:///"+Iv(ivs[i]).path.replace(/\\/gis,"/"),obj.path)
				
				var ivpath:String=Iv(ivs[i]).path.replace(/\\/gis,"/").replace(/.*?(?=\/xml\/)/gis,"")
				
				var objPath:String=obj.path.replace(/.*?(?=\/xml\/)/gis,"")
				
				if(ivpath==objPath){
					
					return true
					
				}
				
				
			}
			
			return false 
			
			
		}
		
		
	 //循环递归出当前模块所有的子模块
	 protected function getSub(obj:Iv){
			
			var xml=workspace+obj.path.replace(/.*?(?=\/xml\/)/gis,"").replace(/\//gis,"\\")
				
			var jsfl=xml.replace(".xml",".jsfl").replace("\\xml\\","\\jsfl\\")
				
			var view=workspace+obj.path.replace(/.*?\/xml\//gis,"/").replace(/\//gis,"\\").replace(".xml",".as")
				
			var arr:Array=view.split("\\")
			var pop=arr.pop()
			arr.push("view")
			arr.push(pop)
			view=arr.join("\\")

			var model=workspace+obj.path.replace(/.*?\/xml\//gis,"/").replace(/\//gis,"\\").replace("View.xml","Model.as")

			arr=model.split("\\")
			pop=arr.pop()
			arr.push("model")
			arr.push(pop)
			model=arr.join("\\")	

			var viewProxy=workspace+obj.path.replace(/.*?\/xml\//gis,"/").replace(/\//gis,"\\").replace(".xml","Proxy.as")
	
			arr=viewProxy.split("\\")
			pop=arr.pop()
			arr.push("viewProxy")
			arr.push(pop)
			viewProxy=arr.join("\\")		
				
				
			var modelProxy=workspace+obj.path.replace(/.*?\/xml\//gis,"/").replace(/\//gis,"\\").replace("View.xml","ModelProxy.as")
	
			arr=modelProxy.split("\\")
			pop=arr.pop()
			arr.push("modelProxy")
			arr.push(pop)
			modelProxy=arr.join("\\")	
				
			var commands=workspace+obj.path.replace(/.*?\/xml\//gis,"/").replace(/\/display\//gis,"/commands/").replace(/\//gis,"\\").replace(".xml","")
			var business=workspace+obj.path.replace(/.*?\/xml\//gis,"/").replace(/\/display\//gis,"/business/").replace(/\//gis,"\\").replace(".xml","")
		
				
			//arr=xml.split("\\")		
			var xmlcopy:String=xml.replace(workspace,Global.workspace)

			//arr=jsfl.split("\\")		
			var jsflcopy:String=jsfl.replace(workspace,Global.workspace)
		
			//arr=view.split("\\")	
			var viewcopy:String=view.replace(workspace,Global.workspace)
			
			//arr=model.split("\\")		
			var modelcopy:String=model.replace(workspace,Global.workspace)
				
			//arr=viewProxy.split("\\")		
			var viewProxycopy:String=viewProxy.replace(workspace,Global.workspace)
				
			//arr=modelProxy.split("\\")		
			var modelProxycopy:String=modelProxy.replace(workspace,Global.workspace)
				
			//arr=commands.split("\\")		
			var commandscopy:String=commands.replace(workspace,Global.workspace)
				
			//arr=business.split("\\")		
			var businesscopy:String=business.replace(workspace,Global.workspace)
				
										
		   subPaths.push(
			   { xml:xml,
		         view:view,
		         model:model,
		         viewProxy:viewProxy,
		         modelProxy:modelProxy,
		         commands:commands,
		         business:business,
				 jsfl:jsfl,
				 
				 xmlcopy:xmlcopy,
				 viewcopy:viewcopy,
				 modelcopy:modelcopy,
				 viewProxycopy:viewProxycopy,
				 modelProxycopy:modelProxycopy,
				 commandscopy:commandscopy,
				 businesscopy:businesscopy,
				 jsflcopy:jsflcopy
				 
			   })
			
		   
		   trace("===================\n"+xml+"\n"+jsfl+"\n"+
		   view+"\n"+
		   model+"\n"+
		   viewProxy+"\n"+
		   modelProxy+"\n"+
		   commands+"\n"+
		   business+"\n"+
		   xmlcopy+"\n"+jsflcopy+"\n"+
		   viewcopy+"\n"+
		   modelcopy+"\n"+
		   viewProxycopy+"\n"+
		   modelProxycopy+"\n"+
		   commandscopy+"\n"+
		   businesscopy+"\n===========================\n")  
		
   
		   var items:Array=ElementControl.getObjs(Iv,obj)
			
		   for(var i=0;i<items.length;i++){
		   
			   getSub(items[i])
		   
		   }
		
		}
		
	 function replaceSource(source:String,regstr:String,newstr:String):String{
		 
		 source=source.replace(new RegExp(RegControl.transform(regstr),"gs"),newstr)	
			 
         return source
			
	 }
	 
	 function jsflrep(str:String,strcopy:String){
		 
		 var reg:RegExp=/.*?(\\jsfl\\)/gis
		 
		 var xmlrepstr:String=str.replace(reg,"")
		 
		 var arr:Array=xmlrepstr.split("\\")
		 
		 arr.pop()
		 
		 xmlrepstr=arr.join(".")
		 
		 var xmlnewstr:String=strcopy.replace(reg,"")
		 
		 var arr:Array=xmlnewstr.split("\\")
		 
		 arr.pop()
		 
		 xmlnewstr=arr.join(".")
		 
		 trace(strcopy,xmlrepstr,xmlnewstr)
		 
		 dojsfl(strcopy,xmlrepstr,xmlnewstr)
		 
		 
		 
	 }
	 function dojsfl(path:String,repstr:String,newstr:String){
			 
			 var res:AbcResponder=new AbcResponder()
			 
			 res.onResult=function(e,path){
				 
				 var source:String=e
				 
				 source=replaceSource(source,workspace.replace(/\\/,"/"),Global.workspace.replace(/\\/,"/"))
				 
				 source=replaceSource(source,repstr,newstr)
				
				 setTimeout(FileControl.saveFile,100,path,source)
				 
			 }
			 
			 new AbcHTTPServices(path,res)
			 
		 }

	 function xmlrep(str:String,strcopy:String){
		  
		  var reg:RegExp=/.*?(?=\\xml\\)/gis

		  var xmlrepstr:String=str.replace(reg,"")
		  
		  var arr:Array=xmlrepstr.split("\\")
		  
		  arr.pop()
		  
		  xmlrepstr=arr.join("/")
		  
		  var xmlnewstr:String=strcopy.replace(reg,"")
		  
		  var arr:Array=xmlnewstr.split("\\")
		  
		  arr.pop()
		  
		  xmlnewstr=arr.join("/")
			  
	      //trace(xmlrepstr,xmlnewstr)
		  
		  doXML(strcopy,xmlrepstr,xmlnewstr)

	  }
	 
	 function doXML(path:String,repstr:String,newstr:String){
			  
			  var res:AbcResponder=new AbcResponder()
			  
			  res.onResult=function(e,path){
				  
				  var source:String=e
				  
				  source=replaceSource(source,repstr,newstr)
				  
				  source=replaceSource(source,repstr.split("/").join(".").replace(/\.xml\./,""),newstr.split("/").join(".").replace(/\.xml\./,""))
				  
				  setTimeout(FileControl.saveFile,100,path,source)
				  
			  }
			  
			  new AbcHTTPServices(path,res)
			  
		  }
	  
	 private var source:String
	 private var sourcepath:String
	 
	 function doview(path:String,repstr:String,newstr:String){
		 
		 var res:AbcResponder=new AbcResponder()
		 
		 res.onResult=function(e,path){
			 
			 if(sourcepath!=path){
				 
				// trace("===============================")
				 
				// trace(sourcepath)
				 
				 //trace(source)
				 
				 //trace("===============================")
				 sourcepath=path;
				 source=e
			 }

			 source=replaceSource(source,repstr,newstr)

			 setTimeout(FileControl.saveFile,100,path,source)
			 
		 }
		 
		 new AbcHTTPServices(path,res)
		 
	 }
	 
	 function viewrep(str:String,strcopy:String){
		 
		 str=str.replace(/\//gis,"\\")
			 
		 strcopy=strcopy.replace(/\//gis,"\\")
		 
		 var reg:RegExp=/.*?(?=\\org\\)/gis
		 
		 var xmlrepstr:String=str.replace(reg,"")
		 
		 var arr:Array=xmlrepstr.split("\\")
		 
		 arr.pop()
			 
		 if(arr.join(".").indexOf("commands")==-1&&arr.join(".").indexOf("business")==-1)arr.pop()
		 
		 xmlrepstr=arr.join(".").replace(/\./,"")
			 
			 
		 var xmlnewstr:String=strcopy.replace(reg,"")
		 
		 var arr:Array=xmlnewstr.split("\\")
			 
		 if(arr.join(".").indexOf(".as")!=-1){
			 
			 arr.pop()
			 arr.pop()
			 
		 }else{
		 
			 arr.pop()
		 }
		 
		 xmlnewstr=arr.join(".").replace(/\./,"")
		 
		 trace("++++++++++++++++++++++\n",str+"\n",strcopy+"\n",xmlrepstr+"\n",xmlnewstr+"\n","\n+++++++++++++++++++++++++")
		 
		 doview(strcopy,xmlrepstr,xmlnewstr)

	 }
		
	 function modifyFile(){
	   
		   for(var i=0;i<subPaths.length;i++){
			   
			   xmlrep(subPaths[i].xml,subPaths[i].xmlcopy)
			   
			   jsflrep(subPaths[i].jsfl,subPaths[i].jsflcopy)
			   
			   rep(subPaths[i].viewcopy,subPaths[i].view,subPaths[i].model,subPaths[i].modelProxy,subPaths[i].viewProxy,subPaths[i].commands,subPaths[i].business)
			   
			   rep(subPaths[i].modelcopy,subPaths[i].view,subPaths[i].model,subPaths[i].modelProxy,subPaths[i].viewProxy,subPaths[i].commands,subPaths[i].business)
			   
			   rep(subPaths[i].modelProxycopy,subPaths[i].view,subPaths[i].model,subPaths[i].modelProxy,subPaths[i].viewProxy,subPaths[i].commands,subPaths[i].business)
			   
			   rep(subPaths[i].viewProxycopy,subPaths[i].view,subPaths[i].model,subPaths[i].modelProxy,subPaths[i].viewProxy,subPaths[i].commands,subPaths[i].business)

			   //方法文件处理
			   
			   var paths:Array=[]
			   
			   var coms:Array=FileControl.readFile(paths,subPaths[i].commandscopy)
				   
			   for(var k=0;k<coms.length;k++){
				   
				  // trace(coms[k].url)

				   rep(coms[k].replace("file:///",""),subPaths[i].view,subPaths[i].model,subPaths[i].modelProxy,subPaths[i].viewProxy,subPaths[i].commands,subPaths[i].business)
				   
			   }
			   
			   paths=[]
			   var buss:Array=FileControl.readFile(paths,subPaths[i].businesscopy)

				   
			   for( k=0;k<buss.length;k++){
				   
				 // trace(buss[k].url)
				   
				   rep(buss[k].replace("file:///",""),subPaths[i].view,subPaths[i].model,subPaths[i].modelProxy,subPaths[i].viewProxy,subPaths[i].commands,subPaths[i].business)
				   
			   }
			   
		   }
		   
		   function rep(copy,view,model,modelProxy,viewProxy,commands,business){
		   
			   viewrep(view,copy)
			   viewrep(model,copy)
			   viewrep(modelProxy,copy)
			   viewrep(viewProxy,copy)
			   viewrep(commands,copy)
			   viewrep(business,copy)

		   }
		   

	 }
		
	 protected override  function menuselectEvent(e:ContextMenuEvent){
			
			var eventName:String=this.getViewName()
			
			var openpath:String=path.replace("/xml/","/")
			
			var type:String=ContextMenuItem(e.currentTarget).caption
			
			var arr:Array=openpath.split("/")
			arr.pop()
			arr.push(StringControl.onewordLower(type.replace(classname,"")))
			
			if(type==classname+"View"){
				
				arr.push(eventName+".as")	
				
			}else if(type==classname+"ViewProxy"){
				
				arr.push(eventName+"Proxy.as")
			}
			if(type==classname+"ModelProxy"){
				
				arr.push(eventName.replace("View","Model")+"Proxy.as")
				
			}else if(type==classname+"Model"){
				
				arr.push(eventName.replace("View","Model")+".as")
				
			}
			
			openpath=arr.join("/")
			
			switch(type){
				
				case "加载模块"+classname:

					getSub(this)
					
					for(var i=0;i<subPaths.length;i++){

						FileControl.copyFile(subPaths[i].xml,subPaths[i].xmlcopy)
						FileControl.copyFile(subPaths[i].jsfl,subPaths[i].jsflcopy)
						FileControl.copyFile(subPaths[i].view,subPaths[i].viewcopy)
						FileControl.copyFile(subPaths[i].model,subPaths[i].modelcopy)
						FileControl.copyFile(subPaths[i].viewProxy,subPaths[i].viewProxycopy)
						FileControl.copyFile(subPaths[i].modelProxy,subPaths[i].modelProxycopy)
						FileControl.copyFile(subPaths[i].commands,subPaths[i].commandscopy)
						FileControl.copyFile(subPaths[i].business,subPaths[i].businesscopy)
					
					}
					
					//modifyFile();
					
					new CreateCommandControl();
					
					break

				
				case classname+"View":
					
				case classname+"Model":
					
				case classname+"ViewProxy":
					
				case classname+"ModelProxy":
					
					var file:File = File.desktopDirectory.resolvePath(openpath);
					file.openWithDefaultApplication();
					
					break
				
				case "编辑":
					
					if(Global.editObj!=scene){
						
						if(path==Iv(Global.editObj).path){
							Alert.show("不能把自己添加到自己内部")
							return;}
						
					}
					
					tab.selectedIndex=0
					
					var iv:Iv=new Iv(path)
					
					setTimeout(function(){
						if(Global.editObj==scene){
							Global.editObj.addChild(iv)
						}else{
							Global.editObj.addChild(iv)
							iv.createMenuItems(["重命名","复制","最上层","最下层","删除"])
						}
						
					},200)
					
					break
				/*
				case "编辑":
				
				tab.selectedIndex=0
				
				ElementControl.cleanObjByType(ShapeBase,scene)
				
				var iv:Iv=new Iv(path)
				
				setTimeout(iv.setObjs,100,scene)
				
				Global.xmlPath=path
				
				sceneTab.label="Scene>>"+Global.xmlPath.split("/")[Global.xmlPath.split("/").length-1]
				
				break
				*/
				case "删除":
					
					ElementControl.cleanObj(this)
					
					break
				
				
				case "添加数据":
					
					var addFolderPannel:ETitlePannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
					addFolderPannel.title=ContextMenuItem(e.currentTarget).caption
					addFolderPannel.userName.text="value : String"
					
					
					//file:///C:/abcTime/xml/org/display/component/album/AlbumView.xml
					//C:\abcTime\org\display\main\model\LineModel.as
					var arr:Array = path.replace("file:///","").replace("/xml/","/").replace("View.xml","Model.as").split("/")
					arr.splice(arr.length-1,0,"model")
					Global.selectPath=arr.join("\\")
					
					break
				
				case "添加异步事件":
					
				case "添加功能事件":
					
					addFolderPannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
					addFolderPannel.title=ContextMenuItem(e.currentTarget).caption
					addFolderPannel.userName.text=""
					
					
					//file:///C:/abcTime/xml/org/display/component/album/AlbumView.xml
					//C:\abcTime\org\display\main\model\LineModel.as
					var arr:Array = path.replace("file:///","").replace("/xml/","/").replace("View.xml","ViewProxy.as").split("/")
					arr.splice(arr.length-1,0,"viewProxy")
					Global.selectPath=arr.join("\\")
					
					
					break
				
				case "发布":
					
					//file:///C:/abcTime/xml/org/display/component/album/AlbumView.xml
					var jsflPath:String=path.replace(/\/xml\//gis,"\/jsfl\/").replace(".xml",".jsfl")
					
					var file:File = File.desktopDirectory.resolvePath(jsflPath);
					file.openWithDefaultApplication();
					
					break
				
				
				case "测试":
					
					//新建模块fla文件
					var sourceFile:File = File.documentsDirectory;
					var path:String=workspace.replace(/\\/gis,"/")+"/org/abcFrame/templete/main.fla"
					sourceFile = sourceFile.resolvePath(path);
					var destination:File = File.documentsDirectory;
					
					destination = destination.resolvePath(workspace.replace(/\\/gis,"/")+"/swf/test/"+this.getViewName()+".fla");
					sourceFile.copyTo(destination, true)
					
					//自动打开新建模块fla文件
					destination.openWithDefaultApplication();
					
					
					setTimeout(function(){
						
						var jsflPath:String=path.replace(/\/xml\//gis,"\/jsfl\/").replace(".xml",".jsfl")
						var file:File = File.desktopDirectory.resolvePath(jsflPath);
						file.openWithDefaultApplication();
						
					},2000)
					
					
					
					break
				
				
				
			}
			
			
			
		}
		
	}
}